<template>
  <el-dialog
    :title="!dataForm.skuId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <!-- <el-form-item label="spuId" prop="spuId">
        <el-input v-model="dataForm.spuId" placeholder="spuId"></el-input>
      </el-form-item> -->
      <el-form-item label="sku名称" prop="skuName">
        <el-input v-model="dataForm.skuName" placeholder="sku名称"></el-input>
      </el-form-item>
      <!-- <el-form-item label="sku介绍描述" prop="skuDesc">
        <el-input
          v-model="dataForm.skuDesc"
          placeholder="sku介绍描述"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="所属分类" prop="catalogId">
        <!-- <el-input
          v-model="dataForm.catalogId"
          placeholder="所属分类id"
        ></el-input> -->
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>
      <el-form-item label="默认图片" prop="skuDefaultImg">
        <!-- <el-input
          v-model="dataForm.skuDefaultImg"
          placeholder="默认图片"
        ></el-input> -->

        <el-col :span="24">
          <!-- <label style="display:block;float:left">选择图集 或</label> -->
          <single-upload
            style="float:left;width: 300px;"
            :showFile="false"
            :listType="'text'"
            v-model="dataForm.skuDefaultImg"
          ></single-upload>
        </el-col>
        <el-col :span="24">
          <el-divider></el-divider>
        </el-col>
      </el-form-item>
      <el-form-item label="标题" prop="skuTitle">
        <el-input v-model="dataForm.skuTitle" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="副标题" prop="skuSubtitle">
        <el-input
          v-model="dataForm.skuSubtitle"
          placeholder="副标题"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item> -->
      <!--<el-form-item label="降价价格" prop="reducedPrice">
        <el-input
          v-model="dataForm.reducedPrice"
          placeholder="降价价格"
        ></el-input>
      </el-form-item>-->
      <el-form-item label="重量" prop="weight">
        <el-input v-model="dataForm.weight" placeholder="重量"></el-input>
      </el-form-item>
      <!-- <el-form-item label="销量" prop="saleCount">
        <el-input v-model="dataForm.saleCount" placeholder="销量"></el-input>
      </el-form-item>
      <el-form-item label="关注数" prop="attentionCount">
        <el-input
          v-model="dataForm.attentionCount"
          placeholder="关注数"
        ></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import MultiUpload from "@/components/upload/multiUpload";
import SingleUpload from "@/components/upload/singleUpload";
import CategoryCascader from "../common/category-cascader";
export default {
  components: {
    MultiUpload,
    SingleUpload,
    CategoryCascader
  },
  watch: {
    catelogPath(path) {
      //监听到路径变化需要查出这个三级分类的分组信息
      console.log("路径变了", path);
      this.attrGroups = [];
      this.dataForm.attrGroupId = "";
      this.dataForm.catelogId = path[path.length - 1];
      if (path && path.length == 3) {
        this.$http({
          url: this.$http.adornUrl(
            `/product/attrgroup/list/${path[path.length - 1]}`
          ),
          method: "get",
          params: this.$http.adornParams({ page: 1, limit: 10000000 })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.attrGroups = data.page.list;
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (path.length == 0) {
        this.dataForm.catelogId = "";
      } else {
        this.$message.error("请选择正确的分类");
        this.dataForm.catelogId = "";
      }
    }
  },
  data() {
    return {
      visible: false,
      catelogPath: [],
      attrGroups: [],
      dataForm: {
        skuId: "",
        spuId: "",
        skuName: "",
        skuDesc: "",
        catalogId: "",
        skuDefaultImg: "",
        skuTitle: "",
        skuSubtitle: "",
        price: "",
        reducedPrice: "",
        weight: "",
        saleCount: "",
        attentionCount: ""
      },
      dataRule: {
        spuId: [{ required: true, message: "spuId不能为空", trigger: "blur" }],
        skuName: [
          { required: true, message: "sku名称不能为空", trigger: "blur" }
        ],
        skuDesc: [
          { required: true, message: "sku介绍描述不能为空", trigger: "blur" }
        ],
        catalogId: [
          { required: true, message: "所属分类id不能为空", trigger: "blur" }
        ],
        skuDefaultImg: [
          { required: true, message: "默认图片不能为空", trigger: "blur" }
        ],
        skuTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        skuSubtitle: [
          { required: true, message: "副标题不能为空", trigger: "blur" }
        ],
        price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
        reducedPrice: [
          { required: true, message: "降价价格不能为空", trigger: "blur" }
        ],
        weight: [{ required: true, message: "重量不能为空", trigger: "blur" }],
        saleCount: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        attentionCount: [
          { required: true, message: "关注数不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.skuId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.skuId) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/skuinfo/info/${this.dataForm.skuId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              console.log(data.skuInfo);
              this.dataForm.skuId = data.skuInfo.skuId;
              this.dataForm.spuId = data.skuInfo.spuId;
              this.dataForm.skuName = data.skuInfo.skuName;
              this.dataForm.skuDesc = data.skuInfo.skuDesc;
              this.dataForm.catalogId = data.skuInfo.catalogId;
              this.dataForm.skuDefaultImg = data.skuInfo.skuDefaultImg;
              this.dataForm.skuTitle = data.skuInfo.skuTitle;
              this.dataForm.skuSubtitle = data.skuInfo.skuSubtitle;
              this.dataForm.price = data.skuInfo.price;
              this.dataForm.reducedPrice = data.skuInfo.reducedPrice;
              this.dataForm.weight = data.skuInfo.weight;
              this.dataForm.saleCount = data.skuInfo.saleCount;
              this.dataForm.attentionCount = data.skuInfo.attentionCount;
              this.catelogPath = data.skuInfo.catelogPath;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/skuinfo/${!this.dataForm.skuId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              skuId: this.dataForm.skuId || undefined,
              spuId: this.dataForm.spuId,
              skuName: this.dataForm.skuName,
              skuDesc: this.dataForm.skuDesc,
              catalogId: this.dataForm.catalogId,
              skuDefaultImg: this.dataForm.skuDefaultImg,
              skuTitle: this.dataForm.skuTitle,
              skuSubtitle: this.dataForm.skuSubtitle,
              price: this.dataForm.price,
              reducedPrice: this.dataForm.reducedPrice,
              weight: this.dataForm.weight,
              saleCount: this.dataForm.saleCount,
              attentionCount: this.dataForm.attentionCount
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
